﻿using System.Text.RegularExpressions;

namespace CodingExercises._41_50;

public class FlattenList
{
    // 展开嵌套的数字列表
    public static List<int> Method(List<List<int>> nestedList)
    {
        if (nestedList is null)
        {
            throw new ArgumentNullException(nameof(nestedList), "Nested list cannot be null.");
        }

        var result = new List<int>();
        foreach (var innerList in nestedList)
        {
            if (innerList is not null)
            {
                foreach (var number in innerList)
                {
                    result.Add(number);
                }
            }
        }

        return result;
    }

    public static List<int> Method2(List<List<int>> nestedList)
    {
        if (nestedList is null)
        {
            throw new ArgumentNullException(nameof(nestedList), "Nested list cannot be null.");
        }

        return nestedList
            .Where(innerList => innerList is not null)
            .SelectMany(innerList => innerList)
            .ToList();
    }
}